/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @filename: read-config.h
 */
#ifndef ____READ_CONFIG_H___
#define ____READ_CONFIG_H___


#include <configmake.h>
#include <server_includes.h>
#include <os.h>

class ConfigValues
{
    ConfigValues(const ConfigValues&) ;

    ConfigValues& operator = (const ConfigValues&) ;

public:
    //All the members of the configuration file
    unsigned int   pageSize;
    int       maxProcs;
    long      maxSysSize;
    uint64_t  maxDbSize;
    int   sysDbKey;
    int   userDbKey;
    char  logFile       [MAX_FILE_PATH_LEN];
    char  dbFile        [MAX_FILE_PATH_LEN];

    char  dbTmpDir      [MAX_FILE_PATH_LEN];
    char  dbPluginDir   [MAX_FILE_PATH_LEN];
    char  dbDataDir     [MAX_FILE_PATH_LEN];
    char  dbShareLibDir [MAX_FILE_PATH_LEN];
    long  mapAddr;
    int   mutexSecs;
    int   mutexUSecs;
    int   mutexRetries;
    int   lockSecs;
    int   lockUSecs;
    int   lockRetries;

    int  cacheId;

    bool isCache;

    char tableConfigFile[MAX_FILE_PATH_LEN];

    bool isTwoWay;
    int  cacheWaitSecs;

    bool isReplication;

    bool isUsingSqlServer;
    /* The port DB server is listening */
    uint32_t port;

    char replConfigFile[MAX_FILE_PATH_LEN];

    int  networkID;

    int  cacheNetworkID;

    long logStoreSize;
    int  nwResponseTimeout;
    int  nwConnectTimeout;
    /* the server id .*/
    int32_t serverId_ ;

    int err_level ;

    ConfigValues()
    {
        pageSize   = COLLAPSAR_PAGE_SIZE;
        /**/
        maxProcs   = 1024;
        maxSysSize = 10485760;

        maxDbSize  = 104857600;
        sysDbKey   = COLLAPSAR_SYS_DBKEY;
        userDbKey  = COLLAPSAR_USER_DBKEY;

        strcpy(logFile, "/tmp/log/log.out");
        strcpy(dbFile, "/tmp/csql/csql.db");

        mapAddr    =400000000;
        mutexSecs  =0;
        mutexUSecs=10;
        mutexRetries = 10;
        lockSecs =0;
        lockUSecs = 10;
        lockRetries = 10;
        cacheId=1;
        isCache = false;
        cacheNetworkID =-1;

        strcpy(tableConfigFile, "/tmp/csql/csqltable.conf");
        isReplication    = false;
        isUsingSqlServer = false;
        port = 5678;
        strcpy(replConfigFile, "/tmp/csql/csqlnw.conf");
        logStoreSize = 10485760;
        networkID=-1;
        nwResponseTimeout=3;
        nwConnectTimeout =5;
        isTwoWay        =true;
        cacheWaitSecs =10;
        serverId_     = 0 ;
        strcpy(dbDataDir,DATADIR)        ;
        if(getenv("TMPDIR")) strcpy(dbTmpDir,getenv("TMPDIR"));
        else
          strcpy(dbTmpDir,"/tmp")          ;
        /*plug-in directory
         *
         **/
        assert(server_home != NULL ); /*We put assert here.*/
        bzero (dbPluginDir,sizeof(dbPluginDir));
        strcpy(dbPluginDir,server_home) ;
        strcat(dbPluginDir,"plugin") ;
        bzero (dbShareLibDir,sizeof(dbShareLibDir));
        strcpy(dbShareLibDir,server_home) ;
        strcat(dbShareLibDir,"share") ;

        err_level = ERRMSG_LVL_INFO ;
    }
}; // ConfigValues

class Config
{
private:
    ConfigValues cVal;

    int readLine(FILE *fp, char * buffer);

    int storeKeyVal(char *key, char *val);

    int validateValues();

    int readAllValues(const char *filename);

    Config& operator = (Config&);
    Config(const Config&);
public:
    Config() {}

    int readConfigFile(const char* _conffile )  ;

    void print();

    inline ConfigValues& getConfigValues() { return cVal ;}

    inline unsigned int getPageSize() { return cVal.pageSize; }

    inline int getMaxProcs() { return cVal.maxProcs; }

    inline long getMaxSysDbSize() { return cVal.maxSysSize; }

    inline long getMaxDbSize() { return cVal.maxDbSize; }

    inline int getSysDbKey() { return cVal.sysDbKey; }

    inline int getUserDbKey() { return cVal.userDbKey; }

    inline char* getLogFile() { return cVal.logFile; }

    inline char* getDbFile()  { return cVal.dbFile; }

    inline long getMapAddress() { return cVal.mapAddr; }

    inline int getMutexSecs() { return cVal.mutexSecs; }

    inline int getMutexUSecs() { return cVal.mutexUSecs; }

    inline int getMutexRetries() { return cVal.mutexRetries; }

    inline int getLockSecs()     { return cVal.lockSecs; }

    inline int getLockUSecs()    { return cVal.lockUSecs; }

    inline int getLockRetries()  { return cVal.lockRetries; }

    inline int getCacheID()      { return cVal.cacheId;}

    inline bool  useCache()       { return cVal.isCache; }

    inline char* getTableConfigFile() { return cVal.tableConfigFile; }

    inline bool useReplication() { return cVal.isReplication; }

    inline uint32_t  getServerPort() { return cVal.port; }

    inline char* getReplConfigFile() { return cVal.replConfigFile; }

    inline long getMaxLogStoreSize() { return cVal.logStoreSize; }

    inline int getNetworkID() { return cVal.networkID; }

    inline int getCacheNetworkID() { return cVal.cacheNetworkID; }

    inline int getNetworkResponseTimeout() { return cVal.nwResponseTimeout; }

    inline int getNetworkConnectTimeout() { return cVal.nwConnectTimeout; }

    inline bool useTwoWayCache() { return cVal.isTwoWay; }

    inline int getCacheWaitSecs() { return cVal.cacheWaitSecs; }

    inline int32_t getServerId() {  return cVal.serverId_;  }

    inline int getErrorLevel()   { return cVal.err_level ;}
    /* the temp directory */
    inline char* getTmpDir () { return cVal.dbTmpDir ;}
    /*  the datafile directory */
    inline char* getDataDir() { return cVal.dbDataDir ;}
    /*  the plug-in directory*/
    inline char* getPluginDir() { return cVal.dbPluginDir;}

    inline char* getShareLibDir() { return cVal.dbShareLibDir ;}
}; // End of Config

class Conf
{

    Conf (const Conf& rhs);

    Conf& operator = (const Conf & rhs) ;
public:
    static Config config;
    Conf(){}

}; // End of Conf


#endif //____READ_CONFIG_H___
